﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
using HsTec.Communication.Share.SqlServerDAL;
using Baimasoft.FrameWork.Base;
using Baimasoft.Manage.SqlServerDAL;

public partial class DocumentManage_Document_Edit : Baimasoft .Web.UI.PageFormTop 
{
    BaimasoftFile MyFile = new BaimasoftFile();
    LogManage MyLog = new LogManage();
    HsTec.Mobile.Green.SqlServerDAL.DataFolder sqldataFolder = new HsTec.Mobile.Green.SqlServerDAL.DataFolder();

    public static string sType = "1", sDocID = "0";

    protected override void InitForm()
    {        
        try
        {
            sType = Request.QueryString["Type"].ToString().Trim();
        }
        catch
        {
            sType = "1";
        }

        try
        {
            sDocID = Request.QueryString["DocID"].ToString().Trim();
        }
        catch
        {
            sDocID = "0";
        }
        if (sDocID.Length < 1)
        {
            sDocID = "0";
        }
        if (sType == "2")
        {
            DataFolder dataFolder = new DataFolder();
            DataTable MyDT = sqldataFolder .GetDataFolderInfo ((Convert.ToInt32(sDocID)));

            foreach (DataRow dr in MyDT.Rows)
            {
                this.FolderName.Text = dr["Name"].ToString().Trim();
                this.HFolderName.Value = dr["Name"].ToString().Trim();
                this.DOrder.Text = dr["DOrder"].ToString().Trim();
            }
            //this.FolderName.ReadOnly = true;             
        }
    }

    /// <summary>
    /// 提交按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GrantBtn1_ServerClick(object sender, EventArgs e)
    {
        try
        {
            string sFolderName = FolderName.Text.ToString().Trim();  //文件夹名称      
            string sHFolderName = HFolderName.Value.ToString().Trim(); //源文件夹名称
            string sOrder = DOrder.Text.ToString().Trim();           //排序号 
            string sPaterID = HID.Value.ToString().Trim();           //父ID 
            string sPaterPath = HPath.Value.ToString().Trim();       //父目录

            string sOPaterPath = HPaterPath.Value.ToString().Trim();       //原文件夹得父目录

            string m_Root = ConfigurationManager.AppSettings["DocumentMange"];  //取Web.Config文件中的配置信息,在配置的时候要注意该盘有写权限
            string str = m_Root + sPaterPath + "\\" + sFolderName;              //写到服务器上的路径

            string ostr = m_Root + sPaterPath;                           //更新文件夹前得路径
            string nstr = m_Root + sOPaterPath + "\\" + sFolderName;            //更新文件夹路径

            string path = sPaterPath + "\\" + sFolderName;                      //当前的父路径                 

            if (sFolderName != "")
            {
                //对文件夹名称进行合法性验证
                if (sType == "2")  //修改
                {
                    int iback = sqldataFolder.UpDataFolderInfo(sOrder, sDocID);
                    if (iback > 0)
                    {
                        if (sHFolderName != sFolderName)  //文件夹名称有改动时,要进行一系列处理
                        {
                            MyFile.MoveDirectory(ostr, nstr);   //更新服务器上得文件夹名称
                            sqldataFolder.DifferentFolderName(sDocID, sFolderName); //更新数据库中得文件夹相关信息;
                        }

                        //日志
                        try
                        {
                            string sPerID = (string)Session["Baimasoft_Pers_ID"];
                            string sContent = "用户" + (string)Session["Baimasoft_User_LogonID"] + "修改文件夹,原文件夹：" + ostr + "现文件夹：" + nstr;
                            string sIP = Page.Request.UserHostAddress;
                            MyLog.InsertLog("3", sPerID, sContent, sIP);
                        }
                        catch
                        {
                            string sContent = "修改文件夹,原文件夹：" + ostr + "现文件夹：" + nstr;
                            string sIP = Page.Request.UserHostAddress;
                            MyLog.InsertLog("3", "0", sContent, sIP);
                        }

                        sqldataFolder.FolderSend();           //更新文件夹目录集合
                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功！');window.close();</script>");
                        return;
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败！');</script>");
                        return;
                    }
                }
                else //新建
                {                   
                    //保存到数据库中
                    try
                    {
                        DataFolder dataFolder = new DataFolder();
                        int iback = dataFolder.GetDataFolderInfos(" name = '" + sFolderName + "' and paterID = '" + sPaterID + "' ").Rows.Count;
                        if (iback < 1)
                        {
                            try
                            {
                                MyFile.CreateDirectory(str);                                     //插入到服务器指定的文件夹下面；
                                sqldataFolder.InsDataFolderInfo(sPaterID, sFolderName, path, sPaterPath, "0", "0", sOrder, "0", DateTime.Now.ToString().Trim(), sPaterID);
                                dataFolder.UPChildNumber(sPaterID);   //更新下级文件夹数目
                            }
                            catch
                            {
                            }

                            //日志
                            try
                            {
                                string sPerID = (string)Session["Baimasoft_Pers_ID"];
                                string sContent = "用户" + (string)Session["Baimasoft_User_LogonID"] + "新建文件夹：" + str;
                                string sIP = Page.Request.UserHostAddress;
                                MyLog.InsertLog("3", sPerID, sContent, sIP);
                            }
                            catch
                            {
                                string sContent = "新建文件夹：" + str;
                                string sIP = Page.Request.UserHostAddress;
                                MyLog.InsertLog("3", "0", sContent, sIP);
                            }

                            sqldataFolder.FolderSend();           //更新文件夹目录集合
                            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('成功创建" + sFolderName + "文件夹！');window.close();</script>");
                            return;
                        }
                        else
                        {
                            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('文件夹" + sFolderName + "已存在！');</script>");
                            return;
                        }
                    }
                    catch
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('创建" + sFolderName + "文件夹失败！');</script>");
                        return;
                    }
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('文件夹名称不能为空！');</script>");
                return;
            }
        }
        catch
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('文件夹名称命名不合法！');</script>");
            return;
        }
    }
    
}
